﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Trippy.UI_Overlay
{
    class MenuWindow
    {
        public class MenuItem
        {
            public string itemText;
            public MenuWindow itemLink;

            public MenuItem(string itemText, MenuWindow itemLink)
            {
                this.itemText = itemText;
                this.itemLink = itemLink;
            }

            public void setText(string text)
            {
                this.itemText = text;
            }
        }

        public enum WindowState { Starting, Active, Ending, Inactive };
        private TimeSpan changeSpan;
        public WindowState windowState { get; protected set; }
        public List<MenuItem> itemList;
        public int selectedItem;
        private SpriteFont spriteFont;
        private double changeProgress;
        public string menuTitle;
        private Texture2D backgroundImage; 
        public bool active;

        public MenuWindow(SpriteFont newSpritefont, string newMenuTitle, Texture2D newBackgroundImage)
        {
            itemList = new List<MenuItem>();
            changeSpan = TimeSpan.FromMilliseconds(800);
            selectedItem = 0;
            changeProgress = 0;
            windowState = WindowState.Inactive;

            this.spriteFont = newSpritefont;
            this.menuTitle = newMenuTitle;
            this.backgroundImage = newBackgroundImage;
        }

        public void AddMenuItem(string itemText, MenuWindow itemLink)
        {
            MenuItem newItem = new MenuItem(itemText, itemLink);
            itemList.Add(newItem);
        }

        public void WakeUp()
        {
            windowState = WindowState.Starting;
        }

        public void Active()
        {
            windowState = WindowState.Active;
        }

        public void Update(double timepassedSinceLastFrame)
        {
            if ((windowState == WindowState.Starting) || (windowState == WindowState.Ending))
                changeProgress += timepassedSinceLastFrame / changeSpan.TotalMilliseconds;

            if (changeProgress >= 1.0f)
            {
                changeProgress = 0.0f;
                if (windowState == WindowState.Starting)
                    windowState = WindowState.Active;
                else if (windowState == WindowState.Ending)
                    windowState = WindowState.Inactive;
            }
        }

        public MenuWindow ProcessInput(KeyboardState lastKeybState, KeyboardState currentKeybState)
        {
            if (lastKeybState.IsKeyUp(Keys.Down) && currentKeybState.IsKeyDown(Keys.Down))
                selectedItem++;

            if (lastKeybState.IsKeyUp(Keys.Up) && currentKeybState.IsKeyDown(Keys.Up))
                selectedItem--;

            if (selectedItem < 0)
                selectedItem = 0;

            if (selectedItem >= itemList.Count)
                selectedItem = itemList.Count - 1;

            if (lastKeybState.IsKeyUp(Keys.Enter) && currentKeybState.IsKeyDown(Keys.Enter))
            {
                windowState = WindowState.Ending;
                if (itemList.Count == 0)
                    return this;
                else
                    return itemList[selectedItem].itemLink;
            }

            else
                return this;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (windowState == WindowState.Inactive)
                return;

            float smoothedProgress = MathHelper.SmoothStep(0,1,(float)changeProgress);

            int verPosition = 300;
            float horPosition = 300;
            float alphaValue;
            float bgLayerDepth;
            Color bgColor;

            switch (windowState)
            {
                case WindowState.Starting:
                    horPosition -= 200 * (1.0f - (float)smoothedProgress);
                    alphaValue = smoothedProgress;
                    bgLayerDepth = 0.5f;
                    bgColor = new Color(new Vector4(1, 1, 1, alphaValue));
                    break;
                case WindowState.Ending:
                    horPosition += 200 * (float)smoothedProgress;
                    alphaValue = 1.0f - smoothedProgress;
                    bgLayerDepth = 1;
                    bgColor = Color.White;
                    break;
                default:
                    alphaValue = 1;
                    bgLayerDepth = 1;
                    bgColor = Color.White;
                    break;
            }

            Color titleColor = new Color(new Vector4(1, 1, 1, alphaValue));
            spriteBatch.Draw(backgroundImage, new Vector2(), null, bgColor, 0, Vector2.Zero, 1, SpriteEffects.None, bgLayerDepth);
            spriteBatch.DrawString(spriteFont, menuTitle, new Vector2(horPosition, 200), titleColor, 0, Vector2.Zero, 1.5f, SpriteEffects.None, 0);

            for (int itemID = 0; itemID < itemList.Count; itemID++)
            {
                Vector2 itemPosition = new Vector2(horPosition, verPosition);
                Color itemColor = Color.White;

                String text = itemList[itemID].itemText;

                if (itemID == selectedItem && active == true)
                {
                    text = "> " + text;
                    itemColor = Color.Red;//new Color(new Vector4(1,0,0,alphaValue));
                }
                else if (itemID == selectedItem)

                {
                    text = "> " + text;
                    itemColor = Color.LightGreen;//new Color(new Vector4(1,0,0,alphaValue));
                }
                   
                else
                {
                    text = "   " + text;
                    itemColor = new Color(new Vector4(1, 1, 1, alphaValue));
                }

                spriteBatch.DrawString(spriteFont, text, itemPosition, itemColor, 0, Vector2.Zero, 1,
                    SpriteEffects.None, 0);

                verPosition += 30;
            }
        }
    }
}
